import { EllipsisOutlined, PlusOutlined } from '@ant-design/icons';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { ProTable, TableDropdown } from '@ant-design/pro-components';
import { Button, Dropdown, Space, Tag } from 'antd';
import { useEffect, useImperativeHandle, useRef, useState } from 'react';
import request from 'umi-request';
import Modelform from './form/model';
export const waitTimePromise = async (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};
export const waitTime = async (time: number = 100) => {
  await waitTimePromise(time);
};
export default (props:any):any => {
const {config} = props 
const [page,setPage] =  useState(1);
const [limit] =  useState(10);
const [total,setTotal] =  useState(0);
const [tableData,setTalbeData] = useState([]);
const [editData,setEditData] = useState({})
const [modalVisit, setModalVisit] = useState(false);
const addData = (n:any)=>{
	let arr:any = [n,...tableData]
	setTalbeData(arr)
}
function editDataFn(obj:any){
	if(obj){
			setEditData(obj)
	}

	setModalVisit(true);
	console.log(1111)
}
useImperativeHandle(props.onRef,()=>{
	return {
		addData,
		editDataFn
	}
})
const submitForm = async (row:any)=>{
	let pmd =  await config.submitForm(row)
	return pmd
}
	const getData = async (params:any={},sort,filter)=>{		
		return request<{
		  data: [];
		}>(config.api, {
		  params,
		}).then((pr:any={}):any=>{
			let kysd = {...params};	
			delete kysd.current 
			delete kysd.pageSize
			delete kysd.created_at
			let arr:[] = pr.data.filter((e:any):any=>{
				let type:boolean = true
				for(let key in kysd){			
					if(kysd[key] && kysd[key]!='undefined'){
						if(type){
							type = kysd[key]===e[key]
						}						
					}			
				}		
				return type
			})
			setTalbeData(arr)	
			setTotal(arr.length)
		});
	}
  return (
  <div>
	<span onClick={editDataFn}>测试</span>

    <ProTable
      columns={config.columns}
      cardBordered
	  dataSource={tableData}
      request={getData}
      editable={{
        type: 'multiple',
      }}
      columnsState={{
        persistenceKey: 'pro-table-singe-demos',
        persistenceType: 'localStorage',
        onChange(value:any) {
          console.log(value)
        },
      }}
      rowKey="id"
      search={{
        labelWidth: 'auto',
      }}
      options={{
        setting: {
          listsHeight: 400,
        },
      }}
      form={{
        // 由于配置了 transform，提交的参与与定义的不同这里需要转化一下
        syncToUrl: (values:any, type:String) => {
          if (type === 'get') {
            return {
              ...values,
              
            };
          }
          return values;
        },
      }}
      pagination={{
        pageSize:limit,
		total:total,
		current:page,
        onChange: (page:any) => {			
			setPage(page)
		},
      }}
      dateFormatter="string"
      headerTitle="高级表格"
      toolBarRender={() => [		  
		  <Modelform 
		  open={modalVisit}
		  onOpenChange={setModalVisit}
		  initialValues={editData}
		  formConfig={config.formConfig} submitForm={submitForm}></Modelform>
      ,
       
      ]}
    />
	  </div>
  );
};